package game.source.gui.menu;

import game.source.gui.GameFrame;
import game.source.gui.GamePanel;
import game.source.gui.ship.GuiPlayerShip;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

/*GameMenu:
 * 	Menu inicial do jogo.
 */
public class GameMenu extends JPanel {

	private JButton			bjogar;			//Botão Jogar
	private JButton			bcreditos;		//Botão Créditos
	private JButton			binstrucoes;	//Botão Instruções
	private JPanel			pbotoes;		//Painel dos botões
	private BufferedImage	img;
	private Border			borda;
	private Border			campos;
	private GameFrame		frame;
	private Font			font;

	public GameMenu(GameFrame frameup) {
		this.frame				= frameup;
		this.bjogar				= new JButton("JOGAR");
		this.bcreditos			= new JButton("CRÉDITOS");
		this.binstrucoes		= new JButton("INSTRUÇÕES");
		this.pbotoes			= new JPanel();
		this.borda				= BorderFactory.createEtchedBorder();
		this.font 				= new Font("Algerian", Font.PLAIN, 20);
		OuvinteBotao ouvinte	= new OuvinteBotao(this.frame);
		this.bjogar.addActionListener(ouvinte);
		this.bcreditos.addActionListener(ouvinte);
		this.binstrucoes.addActionListener(ouvinte);
		this.bjogar.setSize(20, 20);
		this.bcreditos.setSize(20, 20);
		this.bjogar.setFocusable(false);
		this.bjogar.setFont(font);
		this.bcreditos.setFocusable(false);
		this.bcreditos.setFont(font);
		this.binstrucoes.setFocusable(false);
		this.binstrucoes.setFont(font);
		this.pbotoes.setBackground(new Color(0,0,0,0));
		this.pbotoes.setLayout(new FlowLayout(FlowLayout.RIGHT));
		this.pbotoes.add(bjogar);
		this.pbotoes.add(bcreditos);
		this.pbotoes.add(binstrucoes);

		try {
			this.img = ImageIO.read(new File(GameMenu.class.getResource("/images/menus/f16.png").getFile()));
		} catch (Exception e){
			e.printStackTrace();
			System.err.println("Arquivo: " + GameMenu.class.getResource("/images/menus/f16.png") + " não pode ser aberto");
			System.exit(1);
		}


		this.pbotoes.setFont(this.font);
		this.font	= new Font("Algerian", Font.PLAIN, 30);
		this.campos = BorderFactory.createTitledBorder(borda, "Air Hunter", TitledBorder.RIGHT, TitledBorder.TOP, this.font, Color.WHITE);
		this.setBorder(this.campos);
		this.setLayout(new BorderLayout());
		this.add(pbotoes, "South");
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		int x = (this.getWidth() - this.img.getWidth(null)) / 2;
		int y = (this.getHeight() - this.img.getHeight(null)) / 2;
		g.drawImage(this.img, x, y, this);
	}

	class OuvinteBotao implements ActionListener{

		private GameFrame frame;

		public OuvinteBotao(GameFrame frameup){
			this.frame = frameup;
		}

		public void actionPerformed(ActionEvent evt) {
			Object source = evt.getSource();
			if (source == bjogar) {
				this.frame.remove(frame.getGameMenu());
				this.frame.setGamePanel(new GamePanel());
				this.frame.add(this.frame.getGamePanel());
				this.frame.start();
			}
			if(source == bcreditos){
				this.frame.remove(frame.getGameMenu());
				this.frame.setMenuCreditos(new MenuCreditos(this.frame));
				this.frame.add(frame.getMenuCreditos());
				this.frame.start();
			}
			if(source == binstrucoes){
				this.frame.remove(frame.getGameMenu());
				this.frame.setMenuInfo(new MenuInstru(this.frame));
				this.frame.add(frame.getMenuInfo());
				this.frame.start();
			}
		}
	}
}

